package com.polaris.live.viewmodel.live

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.CollectionUtils
import com.polaris.live.common.cacha.DisplayUtil
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.ApiResponse
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.net.homeService
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.resp.back_resp.LiveShowMessageResp
import com.polaris.live.resp.base.ListResp
import com.polaris.live.resp.base.new
import com.polaris.live.resp.event.BannerPromptEvent
import com.polaris.live.widget.refresh.OnListListener
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import org.greenrobot.eventbus.EventBus

/**
 * HomeListViewModel
 *
 * @author Created by 半仙 on 2024/2/27/027 14:27
 */
class HomeLiveViewModel : BaseViewModel(), OnListListener {

    val mListLiveData = MutableLiveData<ResultState<ListResp<Any>>>()
    val mLiveShowLiveData = MutableLiveData<ResultState<LiveShowMessageResp>>()

    private var bannerPosition = 0
    var pageType = DisplayUtil.LIVE_PARTY
    var pageNumber = 0

    override fun onRefresh() {
        pageNumber = 0
        getList(0)
    }

    override fun onLoadMore(pageIndex: Int) {
        pageNumber = pageIndex
        getList(pageIndex)
    }

    private fun getList(pageIndex: Int) {
        request({
            if (pageIndex == 0) {
                coroutineScope {
                    val listDef = async { fetchList(pageIndex) }
                    val bannerDef = async { homeService.getBannerListV2(CommonConst.BannerType.BANNER_PARTY_PAGE) }

                    val listResp = listDef.await()
                    val bannerResp = bannerDef.await()

                    if (bannerResp.isSuccess()) {
                        EventBus.getDefault().post(BannerPromptEvent(bannerResp.result?.notification))
                    }

                    val list = listResp.result?.list?.toMutableList<Any>()?.apply {
                        val bannerBean = ListWithBannerBean(bannerResp.result?.banner ?: emptyList())
                        if (bannerPosition >= size) {
                            add(bannerBean)
                        } else {
                            add(bannerPosition, bannerBean)
                        }
                    } ?: emptyList()
                    if (CollectionUtils.isEmpty(bannerResp.result?.banner)) {
                        listResp.new(listResp.result?.list)
                    } else {
                        listResp.new(list)
                    }
                }
            } else {
                fetchList(pageIndex).let { it.new(it.result?.list) }
            }
        }, mListLiveData)
    }

    private suspend fun fetchList(pageIndex: Int): ApiResponse<ListResp<DisplayUserBean>> {
        return when (pageType) {
            DisplayUtil.LIVE_PARTY -> fetchPartyList(pageIndex)
            DisplayUtil.LIVE_LIVE -> fetchLiveList(pageIndex)
            else -> fetchPartyList(pageIndex)
        }
    }

    private suspend fun fetchPartyList(pageIndex: Int): ApiResponse<ListResp<DisplayUserBean>> {
        return homeService.getHomePartyList(pageIndex)
    }

    private suspend fun fetchLiveList(pageIndex: Int): ApiResponse<ListResp<DisplayUserBean>> {
        return homeService.getLivesList(pageIndex)
    }

    fun livesShowMessage() {
        request({ homeService.livesShowMessage() }, mLiveShowLiveData)
    }
}